package com.qitmiaojie.controller;

import com.qitmiaojie.controller.utils.GeoResult;
import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.pojo.Leave;
import com.qitmiaojie.pojo.dto.Examine;
import com.qitmiaojie.service.LeaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
public class LeaveController {

    @Autowired
    private LeaveService leaveService;

    /**
     * 审批管理  获取所有信息
     * @param name
     * @param id
     * @param examine
     * @param currentPage
     * @param size
     * @return
     */
    @GetMapping("/Leave")
    public Result getLeave(@RequestParam(name = "Name",required = false) String name,
                           @RequestParam(name = "Id",required = false) String id,
                           @RequestParam(name = "Examine",required = false) String examine,
                           @RequestParam(name = "CurrentPage",required = false)Integer currentPage,
                           @RequestParam(name = "Size",required = false)Integer size){
        log.info("{},{},{},{},{}",name,id,examine,currentPage,size);

        return leaveService.getPage(name,id,examine,currentPage,size);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/Leave/{id}")
    public Result getLeaveById(@PathVariable Integer id) {
        return Result.success(leaveService.getById(id));
    }

    /**
     * 删除与 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/Leave")
    public Result getLeaveById(@RequestParam List<Integer> ids) {
        log.info(ids.toString());
        return Result.success(leaveService.removeByIds(ids));
    }

    /**
     * 编辑请假信息
     * @param leave
     * @return
     */
    @PutMapping("/Leave")
    public Result updateLeaveById(@RequestBody Leave leave){
        return leaveService.updateLeave(leave);
    }

    /**
     * 高德地图api 查询地址
     * @param address
     * @return
     */
    @GetMapping("/Address/{address}")
    public Result getAddressByGeography(@PathVariable String address){
        return leaveService.getByGeography(address);
    }

    /**
     * 请假
     * @return
     */
    @PostMapping("/examine")
    public Result examine(@RequestBody Examine examine){
        return leaveService.addExamine(examine);
    }
    @PutMapping("/examine/{id}/{examine}")
    public Result updateExamineById(@PathVariable("id") Integer id,@PathVariable("examine") String examine){
        return leaveService.updateExamineById(id,examine);
    }

    /**
     * 今日出校 入校 出省...
     * @return
     */
    @GetMapping("/StudentStream")
    public Result QueryStudentStream(){
        return leaveService.getStudentStream();
    }

    /**
     * 一周情况
     * @return
     */
    @GetMapping("/LastWeek")
    public Result QueryLastWeek(){
        return leaveService.getLastWeek();
    }

    /**
     * 导出表格
     * @param request
     * @param response
     */
    @GetMapping("/downloadExcel")
    public void downloadExcel(HttpServletRequest request,HttpServletResponse response){
      /*  List<LeaveRecord> recordList = leaveMapper.selectLeaveRecordAll();
        EasyExcelUtil.writeExcel(request,response,recordList,LeaveRecord.class,"账户");*/
        leaveService.downLoadExcelAll(response);
    }

    /**
     * 今日流动
     * @return
     */
    @GetMapping("/DayTime")
    public Result getDayTime(){
       return leaveService.getDayTime();
    }

    /**
     * 首页高德地图流出展示
     * @return
     */
    @GetMapping("/GeographyJson")
    public GeoResult getGeographyJson(){
        return leaveService.getGeographyJson();
    }
}
